﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Modules.quest.Managers.*;
    import Display.*;
    import GameMode.*;
    import Display.Toaster.*;
    import Modules.guide.ui.*;
    import Engine.*;

    public class Bridge extends ParkingLot {

        public static const MAXBRIDGELENGTH:int = 30;
        public static const MAXBRIDGESEGMENTS:int = 10;
        public static const BRIDGE_OBJECT_TEMP_ID_START:Number = 71000;

        protected var m_bridgeSquareSize:Vector3;
        protected var m_validBridgeStart:Boolean;
        protected var m_expansionRect:Rectangle;
        protected var m_validExpansionStart:Boolean;
        protected var m_unlockExpansionPoint:Vector3;
        protected var m_unlockExpansionRect:Rectangle;
        protected var m_validBridgeEnd:Boolean;
        protected var m_bridgeEndSquare:Vector3;
        protected var m_leftBridgeItem:Item;
        protected var m_centerBridgeItem:Item;
        protected var m_rightBridgeItem:Item;
        private var m_loadedPartImages:Boolean = false;
        protected var m_leftBridgeImage:ItemImageInstance;
        protected var m_centerBridgeImage:ItemImageInstance;
        protected var m_rightBridgeImage:ItemImageInstance;
        protected var m_bridgeParts:Array;
        protected var m_toolTipOffset:Vector2;

        public function Bridge(_arg1:String=null){
            this.m_bridgeEndSquare = new Vector3();
            this.m_bridgeParts = [];
            super(_arg1);
            this.m_validBridgeStart = false;
            this.m_expansionRect = new Rectangle();
            this.m_unlockExpansionRect = new Rectangle();
            this.m_unlockExpansionPoint = new Vector3();
            this.m_bridgeSquareSize = m_size.clone();
            this.m_leftBridgeItem = Global.gameSettings.getItemByName(m_item.bridge.leftPart);
            this.m_centerBridgeItem = Global.gameSettings.getItemByName(m_item.bridge.centerPart);
            this.m_rightBridgeItem = Global.gameSettings.getItemByName(m_item.bridge.rightPart);
            this.m_leftBridgeImage = null;
            this.m_centerBridgeImage = null;
            this.m_rightBridgeImage = null;
        }
        public static function isBridgeInValidExpansionByName(_arg1:String, _arg2:int, _arg3:int):Boolean{
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            return (Bridge.isBridgeInValidExpansionByItem(_local4, _arg2, _arg3));
        }
        public static function isBridgeInValidExpansionByItem(_arg1:Item, _arg2:int, _arg3:int):Boolean{
            var _local8:String;
            var _local9:Array;
            var _local10:Number;
            var _local11:Number;
            var _local4:Vector2 = new Vector2(_arg2, _arg3);
            var _local5:Rectangle = Global.world.m_territory.getClosestExpansionRect(_local4);
            var _local6:Array = _arg1.bridge.validBuildExpansions.split(";");
            var _local7:int;
            while (_local7 < _local6.length) {
                _local8 = _local6[_local7];
                _local9 = _local8.split("|");
                _local10 = int(_local9[0]);
                _local11 = int(_local9[1]);
                if ((((_local5.x == _local10)) && ((_local5.y == _local11)))){
                    return (true);
                };
                _local7++;
            };
            return (false);
        }
        public static function getGrantedExpansionPointByName(_arg1:String, _arg2:int, _arg3:int):Vector3{
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            return (Bridge.getGrantedExpansionPointByItem(_local4, _arg2, _arg3));
        }
        public static function getGrantedExpansionPointByItem(_arg1:Item, _arg2:int, _arg3:int):Vector3{
            var _local8:String;
            var _local9:Array;
            var _local10:Number;
            var _local11:Number;
            var _local4:Vector2 = new Vector2(_arg2, _arg3);
            var _local5:Rectangle = Global.world.m_territory.getClosestExpansionRect(_local4);
            var _local6:Array = _arg1.bridge.grantedExpansionsOnPlace.split(";");
            var _local7:int;
            while (_local7 < _local6.length) {
                _local8 = _local6[_local7];
                _local9 = _local8.split("|");
                _local10 = int(_local9[0]);
                _local11 = int(_local9[1]);
                if ((((_local5.x == _local10)) && ((_local5.y == _local11)))){
                    return (new Vector3(int(_local9[2]), int(_local9[3]), 0));
                };
                _local7++;
            };
            return (null);
        }
        public static function getBridgeEndPointByName(_arg1:String, _arg2:int, _arg3:int):Vector3{
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            return (Bridge.getBridgeEndPointByItem(_local4, _arg2, _arg3));
        }
        public static function getBridgeEndPointByItem(_arg1:Item, _arg2:int, _arg3:int):Vector3{
            var _local4:Vector3 = new Vector3(_arg2, _arg3, 0);
            var _local5:Boolean;
            var _local6:int = ((_arg3 - _local4.y) + _arg1.sizeY);
            var _local7:Rectangle = new Rectangle(_local4.x, _local4.y, _arg1.sizeX, _arg1.sizeY);
            while (_local6 <= MAXBRIDGELENGTH) {
                _local4.y = (_local4.y - _arg1.sizeY);
                _local7.y = _local4.y;
                if (((Global.world.citySim.waterManager.positionOnValidLand(_local7)) && (_local5))){
                    break;
                };
                _local5 = true;
            };
            if (_local6 > MAXBRIDGELENGTH){
                _local4.x = _arg2;
                _local4.y = _arg3;
            };
            return (_local4);
        }
        public static function placeBridgePart(_arg1:String, _arg2:Number, _arg3:Number, _arg4:Number=0):BridgePart{
            var _local5:BridgePart = new BridgePart(_arg1);
            _local5.setPosition(Math.round(_arg2), Math.round(_arg3), 0);
            _local5.setDirection(Math.round(_arg4));
            _local5.setOuter(Global.world);
            _local5.setVisible(true);
            _local5.attach();
            return (_local5);
        }

        override public function isSellable():Boolean{
            return (false);
        }
        override public function canBeDragged():Boolean{
            return (false);
        }
        override public function canBeRotated():Boolean{
            return (false);
        }
        public function get isValidBridgeStart():Boolean{
            return (this.m_validBridgeStart);
        }
        public function get isValidBridgeEnd():Boolean{
            return (this.m_validBridgeEnd);
        }
        public function get isValidBridgeExpansionStart():Boolean{
            return (this.m_validExpansionStart);
        }
        public function get expansionRectangle():Rectangle{
            return (this.m_expansionRect);
        }
        public function get freeExpansionZone():Rectangle{
            return (this.m_unlockExpansionRect);
        }
        public function get endSquare():Vector3{
            return (this.m_bridgeEndSquare);
        }
        private function overrideEndSquare(_arg1:int, _arg2:int):void{
            this.m_bridgeEndSquare.x = _arg1;
            this.m_bridgeEndSquare.y = _arg2;
            this.m_bridgeEndSquare.z = 0;
        }
        public function get endRectangle():Rectangle{
            return (new Rectangle(this.m_bridgeEndSquare.x, this.m_bridgeEndSquare.y, this.m_bridgeSquareSize.x, this.m_bridgeSquareSize.y));
        }
        public function get bridgeLength():int{
            return (((this.m_bridgeEndSquare.y - m_position.y) + this.m_bridgeSquareSize.y));
        }
        public function get bridgeRectangle():Rectangle{
            return (new Rectangle(m_position.x, m_position.y, this.m_bridgeSquareSize.x, this.bridgeLength));
        }
        public function get grantedExpansion():Vector3{
            return (this.m_unlockExpansionPoint);
        }
        override public function loadObject(_arg1:Object):void{
            var _local3:Rectangle;
            var _local4:BridgePart;
            var _local5:Rectangle;
            super.loadObject(_arg1);
            this.overrideEndSquare(int(_arg1.endPosition.x), int(_arg1.endPosition.y));
            var _local2:Array = Global.world.getObjectsByClass(BridgePart);
            if (_local2.length){
                _local3 = new Rectangle(getPosition().x, (getPosition().y - getItem().bridge.bridgeBoundary.y), getItem().bridge.bridgeBoundary.x, getItem().bridge.bridgeBoundary.y);
                for each (_local4 in _local2) {
                    if ((_local4 is BridgePart)){
                        _local5 = new Rectangle(_local4.getPosition().x, _local4.getPosition().y, _local4.getSize().x, _local4.getSize().y);
                        if (_local3.intersects(_local5)){
                            this.addBridgePart(_local4);
                        };
                    };
                };
            };
        }
        public function addBridgePart(_arg1:BridgePart):void{
            var part:* = _arg1;
            part.bridgeOwner = this;
            this.m_bridgeParts.push(part);
            this.m_bridgeParts.sort(function (_arg1:BridgePart, _arg2:BridgePart):Number{
                if (_arg1.getPosition().y > _arg2.getPosition().y){
                    return (-1);
                };
                if (_arg1.getPosition().y < _arg2.getPosition().y){
                    return (1);
                };
                return (0);
            });
        }
        public function getRightBridgePart():BridgePart{
            if (this.m_bridgeParts.length == 0){
                return (null);
            };
            return ((this.m_bridgeParts[(this.m_bridgeParts.length - 1)] as BridgePart));
        }
        override public function setPosition(_arg1:Number, _arg2:Number, _arg3:Number=0):void{
            super.setPosition(_arg1, _arg2, _arg3);
        }
        override public function rotate():void{
            super.rotate();
            Global.world.citySim.roadManager.updateAllRoadTiles();
            Global.world.citySim.roadManager.updateRoads(this);
        }
        override protected function updateAdjacent():void{
            var _local1:int;
            var _local2:int;
            var _local3:int;
            if (m_adjacentRoads != null){
                return;
            };
            initalUpdateAdjacentCalculations();
            for each (_local1 in m_adjacentStretch) {
                m_adjacentRoads[_local1] = null;
            };
            m_adjacentStretch = [];
            for each (_local1 in m_adjacentTight) {
                m_adjacentRoads[_local1] = null;
            };
            m_adjacentTight = [];
            _local2 = 0;
            while (_local2 < m_adjacentRoads.length) {
                _local3 = _local2;
                if ((((_local3 == LEFT)) || ((_local3 == RIGHT)))){
                    m_adjacentRoads[_local3] = null;
                    m_adjacentPartial.splice(_local2, 1);
                };
                _local2++;
            };
        }
        override public function drawDisplayObject():void{
            var _local1:GMPlaceBridge;
            super.drawDisplayObject();
            if (this.isCurrentImageLoading()){
                return;
            };
            if ((Global.world.getTopGameMode() is GMPlaceBridge)){
                _local1 = (Global.world.getTopGameMode() as GMPlaceBridge);
                _local1.setupGhostBridgeImage();
            };
            if (!this.m_validBridgeEnd){
                return;
            };
        }
        override public function getToolTipHeader():String{
            return (ZLoc.t("Items", "bridge_standard_friendlyName"));
        }
        override public function getToolTipScreenOffset():Vector2{
            var _local2:int;
            var _local3:Vector3;
            var _local4:Point;
            var _local5:Vector3;
            var _local6:Point;
            var _local7:Number;
            var _local1:Array = this.bridgeParts;
            this.m_toolTipOffset = (this.m_toolTipOffset) ? this.m_toolTipOffset : new Vector2();
            if (_local1.length){
                _local2 = Global.gameSettings.getNumber("maxZoom");
                _local3 = getToolTipPosition();
                _local4 = IsoMath.tilePosToPixelPos(_local3.x, _local3.y);
                _local4 = IsoMath.viewportToStage(_local4);
                _local5 = (_local1[(_local1.length - 1)] as BridgePart).getPosition();
                _local6 = IsoMath.tilePosToPixelPos(_local5.x, _local5.y);
                _local6 = IsoMath.viewportToStage(_local6);
                _local7 = (((_local6.x - _local4.x) * GlobalEngine.viewport.getZoom()) / _local2);
                this.m_toolTipOffset.x = (_local7 / 2);
            };
            return (this.m_toolTipOffset);
        }
        override public function cornerShouldAdjust(_arg1:Road, _arg2:int):Boolean{
            return (false);
        }
        override public function shouldAdjust(_arg1:Road, _arg2:int):Boolean{
            return (false);
        }
        override public function prepareToDrop(_arg1:Vector3, _arg2:Vector2):Road{
            return (this);
        }
        override public function isPositionValid():Boolean{
            return (false);
        }
        public function get bridgeParts():Array{
            return (this.m_bridgeParts);
        }
        public function isValidBridgePosition():Boolean{
            var _local1:Boolean = Bridge.isBridgeInValidExpansionByItem(m_item, m_position.x, m_position.y);
            this.m_validExpansionStart = _local1;
            this.m_validBridgeStart = _local1;
            var _local2:Boolean;
            var _local3:Boolean;
            this.m_validBridgeEnd = false;
            this.m_unlockExpansionRect = null;
            if (m_position.x < m_item.bridge.minX){
                return (false);
            };
            if (m_position.x > m_item.bridge.maxX){
                return (false);
            };
            if (!_local1){
                return (false);
            };
            this.m_unlockExpansionPoint = Bridge.getGrantedExpansionPointByItem(m_item, m_position.x, m_position.y);
            this.m_unlockExpansionRect = null;
            if (this.m_unlockExpansionPoint){
                this.m_unlockExpansionRect = Global.world.m_territory.getClosestExpansionRect(new Vector2(this.m_unlockExpansionPoint.x, this.m_unlockExpansionPoint.y));
            };
            if (!this.checkPlacementRequirements(m_position.x, m_position.y)){
                this.m_validBridgeStart = false;
                return (false);
            };
            this.m_bridgeEndSquare = Bridge.getBridgeEndPointByItem(m_item, m_position.x, m_position.y);
            this.m_bridgeEndSquare = Bridge.getBridgeEndPointByItem(m_item, m_position.x, m_position.y);
            if (this.m_bridgeEndSquare.equals(m_position)){
                return (false);
            };
            return (_local1);
        }
        override public function onBuildingConstructionCompleted_PostServerUpdate():void{
            var _local3:String;
            var _local4:int;
            var _local5:int;
            var _local6:String;
            Global.player.setAllowRightSideBuild();
            GameQuestCallbacks.cycleAnnouncers();
            var _local1:int;
            while (_local1 < this.getItem().bridge.bridgeParts.length) {
                _local3 = this.getItem().bridge.bridgeParts[_local1].type;
                _local4 = this.getItem().bridge.bridgeParts[_local1].x;
                _local5 = this.getItem().bridge.bridgeParts[_local1].y;
                _local6 = "";
                if (_local3 == "left"){
                    _local6 = this.getItem().bridge.leftPart;
                };
                if (_local3 == "center"){
                    _local6 = this.getItem().bridge.centerPart;
                };
                if (_local3 == "right"){
                    _local6 = this.getItem().bridge.rightPart;
                };
                this.addBridgePart(placeBridgePart(_local6, _local4, _local5));
                _local1++;
            };
            Global.world.citySim.roadManager.updateAllRoadTiles();
            this.fireBridgeMissiles(this, 0);
            var _local2:BridgePart = this.getRightBridgePart();
            if (_local2){
                this.fireBridgeMissiles((_local2 as MapResource), 3000);
            };
            Sounds.play("peopleMoveIn");
        }
        private function fireBridgeMissiles(_arg1:MapResource, _arg2:int=0, _arg3:Event=null):void{
            var inputMapResource:* = _arg1;
            var timerOffset:int = _arg2;
            var e = _arg3;
            TimerUtil.callLater(function ():void{
                Sounds.play("cruise_fireworks");
            }, timerOffset);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, timerOffset);
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 300));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 500));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 700));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 900));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 1000));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 1200));
            TimerUtil.callLater(function ():void{
                Sounds.play("cruise_fireworks");
            }, (timerOffset + 1500));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 1600));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 1300));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 1900));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 2100));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 2300));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 2500));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 3000));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 3200));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 3600));
            TimerUtil.callLater(function ():void{
                MapResourceEffectFactory.createEffect(inputMapResource, EffectType.FIREWORK_BALLOONS);
            }, (timerOffset + 3300));
        }
        override public function notifyUserForExpansionsOnPlace(_arg1:Rectangle):void{
            var expansionRect:* = _arg1;
            UI.popupLock();
            var expansionOutline:* = new GuideTile(new Vector3(expansionRect.x, expansionRect.y), expansionRect.width, expansionRect.height);
            expansionOutline.drawGuideTile(Constants.COLOR_VALID_PLACEMENT);
            expansionOutline.blinkTile(Global.gameSettings.getNumber("freeExpansionBlinkerTime", 0.75), Global.gameSettings.getInt("freeExpansionNumBlinks", 5), function ():void{
                UI.popupUnlock();
            });
            var toaster:* = new ItemToaster("", ZLoc.t("Dialogs", "ToasterFreeExpansion"), Global.getAssetURL("assets/dialogs/toaster_edgar.png"));
            toaster.duration = 10000;
            Global.ui.toaster.show(toaster);
        }

    }
}//package Classes 
